<?php
namespace app\manage\model;

use think\Model;

class TaskModelModel extends Model{
	//表名
	protected $table = 'ly_task_model';
	
	/**
	 * 添加模板
	 */
	public function add(){
		if (!request()->isAjax()) return '提交失败';
		$param = input('param.');
		//数据验证
		$validate = validate('app\manage\validate\Task');
		if (!$validate->scene('molde')->check($param)) return $validate->getError();
		
		$iscount = $this->where('task_class',$param['task_class'])->count();
		if($iscount){
			return '模板已存在';
		}

		if (isset($param['finish_condition']) and $param['finish_condition']) $param['finish_condition'] 	= json_encode(array_keys($param['finish_condition']));

		if (isset($param['task_step']) and $param['task_step']) $param['task_step']               			= json_encode(array_merge($param['task_step']),true);
		if (isset($param['examine_demo']) and $param['examine_demo']) $param['examine_demo']         		= json_encode($param['examine_demo'],true);

		$param['end_time']                                         = strtotime($param['end_time']);
		$param['add_time']                                         = time();
		$param['surplus_number']								   = $param['total_number'];

		// 流水 任务金额

		$res = $this->allowField(true)->save($param);
		if (!$res) return '添加失败';

		//添加操作日志
		model('Actionlog')->actionLog(session('manage_username'),'添加模板：模板为'.$param['name'],1);

		return 1;
	}


	/**
	 * 编辑模板
	 */
	public function edit(){
		if (!request()->isAjax()) return '提交失败';

		$param = input('param.');
		
		if (isset($param['type']) && $param['type']){
			
			$data	= $this->where('task_class', $param['id'])->find();
			
			if(!$data){
				return 0;
			}

			if($data['examine_demo']){
				$data['examine_demo'] 	  = json_decode($data['examine_demo'], true);
			}else{
				$data['examine_demo'] 	  = array();
			}
			
			if($data['task_step']){
				$data['task_step']        = json_decode($data['task_step'], true);
			}else{
				$data['task_step']		  =	array();
			}
			
			if($data['finish_condition']){
				$data['finish_condition']         = json_decode($data['finish_condition'], true);
			}else{
				$data['finish_condition']		  =	array();
			}
			$data['end_time']					  =	date('Y-m-d', $data['end_time']);
			unset($data['id']);
			unset($data['name']);
			unset($data['name']);
			return $data;
		
		}
		//数据验证
		$validate = validate('app\manage\validate\Task');
		
		if (!$validate->scene('molde')->check($param)) return $validate->getError();

		$id = $param['id'];
		unset($param['id']);
		if (isset($param['finish_condition']) && $param['finish_condition']) $param['finish_condition'] = json_encode(array_keys($param['finish_condition']));
		if (isset($param['examine_demo']) && $param['examine_demo']) $param['examine_demo']         	= json_encode($param['examine_demo'],true);
		if (isset($param['task_step']) && $param['task_step']) $param['task_step']               		= json_encode(array_merge($param['task_step']),true);
		$param['end_time']                                         										= strtotime($param['end_time']);

		$taskInfo	= $this->where('id', $id)->find();
		
		if(!$taskInfo){
			return '修改失败';
		}

		// 如果是修改任务的领取数量，则必须修改剩余数量——————————————————————————————
		if($param['total_number'] && $param['total_number'] < $taskInfo['total_number']){	// 判断新数量必须大于原数量
			return '修改失败';
		}

		if($param['total_number'] && $param['total_number'] > $taskInfo['total_number']){
			$param['surplus_number']	= $param['total_number'] - $taskInfo['receive_number'];
		}

		$res = $this->allowField(true)->save($param, ['id'=>$id]);
		if (!$res) return '修改失败';
		//添加操作日志
		model('Actionlog')->actionLog(session('manage_username'),'修改模板：标题为'.$param['name'],1);

		return 1;
	}
	/**
		删除模板
	**/
	
	public function del(){
		if (!request()->isAjax()) return '提交失败';
		$param = input('param.');
		if (!$param) return '提交失败';

/*		if (isset($param['data']) && $param['data']) { // 批量删除
			foreach ($param['data'] as $key => $value) {
				// 提取信息
				$taskInfo = $this->where('id', $value['id'])->find();
				if ($taskInfo && is_object($taskInfo)) $taskInfo = $taskInfo->toArray();
				// 删除图片
				if ($taskInfo['examine_demo']) {
					$taskInfo['examine_demo'] = json_decode($taskInfo['examine_demo'], true);
					foreach ($taskInfo['examine_demo'] as $key => $value) {
						@unlink('.'.$value);
					}
				}
				// 删除图片
				if ($taskInfo['task_step']) {
					$taskInfo['task_step'] = json_decode($taskInfo['task_step'], true);
					foreach ($taskInfo['task_step'] as $key => $value) {
						@unlink('.'.$value['img']);
					}
				}

				$res[] = $this->where('id', $value['id'])->delete();
			}
		} elseif (isset($param['id']) && $param['id']) { // 删除单个
			// 提取信息
			$taskInfo = $this->where('id', $param['id'])->find();
			if ($taskInfo && is_object($taskInfo)) $taskInfo = $taskInfo->toArray();

			// 删除图片
			if ($taskInfo['examine_demo']) {
				$taskInfo['examine_demo'] = json_decode($taskInfo['examine_demo'], true);
				foreach ($taskInfo['examine_demo'] as $key => $value) {
					@unlink('.'.$value);
				}
			}
			// 删除图片
			if ($taskInfo['task_step']) {
				$taskInfo['task_step'] = json_decode($taskInfo['task_step'], true);
				foreach ($taskInfo['task_step'] as $key => $value) {
					@unlink('.'.$value['img']);
				}
			}*/

			$res = $this->where('id', $param['id'])->delete();
			if (!$res) return '删除失败';
		/*} else {
			return '提交失败';
		}*/

		return 1;
	}

}